package com.cxhome.cxinfox.productvas;

import java.util.List;

import com.cxhome.cxinfox.cxdepartment.DepartmentService;
import com.cxhome.cxinfox.productinfo.ProductInfoService;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.product.Product;
import com.traffic.managent.model.product.ProductInfo;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.StringUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 本地产品管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/productvas")
public class ProductController extends CxBaseController {

	private static final String path = "/pages/system/productvas/productvas_";

	public void index() {
		list();
	}

	public void list() {
		Product model = getModelByAttr(Product.class);
		StringBuilder orderSql = new StringBuilder("");

		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql.append(" order by  ss.createTime desc");
		} else {
			orderSql.append(" order by ss.").append(orderBy.trim());
		}

		Page<Product> page = ProductService.service.paginate(model, getPaginator(), orderSql);

		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}
//	public void list() {
//		Product model = getModelByAttr(Product.class);
//		String select = "SELECT ss.*, REPLACE( (  SELECT  group_concat(  concat(  cp.providerName, "
//				+ " '----',  cpi.productName ,'----',cpv.level )  )  FROM  cx_product_vasproduct cpv "
//				+ " LEFT JOIN cx_product_info cpi ON cpv.pid = cpi.ids "
//				+ " LEFT JOIN cx_provider cp ON cpi.providerId = cp.ids  WHERE  cpv.vasPid = ss.ids "
//				+ " AND cpv.`status` = '0'  ),',','<br /><br />') AS deploys, (  SELECT  COUNT(1)  FROM "
//				+ " cx_product_vasproduct cpv  LEFT JOIN cx_product_info cpi ON cpv.pid = cpi.ids "
//				+ " LEFT JOIN cx_provider cp ON cpi.providerId = cp.ids  WHERE  cpv.vasPid = ss.ids "
//				+ " AND cpv.`status` = '0'  ) AS deployCount ";
//		
//		SQLUtils sql = new SQLUtils(" FROM  cx_product_vas ss  WHERE ss.`status` = '0' ");
//		
//		if (model.getAttrValues().length != 0) {
//			sql.whereLike("ss.ids", model.getStr("ids"));
//			sql.whereLike("ss.vasProdName", model.getStr("vasProdName"));
//		}
//		
//		// 排序
//		String orderBy = getBaseForm().getOrderBy();
//		if (StringUtils.isEmpty(orderBy)) {
//			sql.append(" order by  ss.createTime desc");
//		} else {
//			sql.append(" order by ss.").append(orderBy);
//		}
//		Page<Product> page = Product.dao.paginate(getPaginator(), select, sql.toString().toString());
//		
//		setAttr("page", page);
//		setAttr("attr", model);
//		render(path + "list.html");
//	}

	/**
	 * 跳转新增页面
	 */
	public void add() {
		setAttr("departSelect", DepartmentService.service.selectDepart(""));
		render(path + "add.html");
	}

	public void view() {
		Product model = Product.dao.findById(getPara());

		setAttr("model", model);
		render(path + "view.html");
	}

	/**
	 * 删除
	 */
	public void delete() {
		Product.dao.deleteById(getPara());
		list();
	}

	public void edit() {
		Product model = Product.dao.findById(getPara());
		setAttr("model", model);
		render(path + "edit.html");
	}

	/**
	 * 保存：更新或者添加
	 */
	public void save() {
		String ids = getPara();
		Product model = getModel(Product.class);
		String userid = getSessionUser().getUseriD();
		ProductService.service.saveOrUpdate(ids, model, userid);
		renderMessage("保存成功");
	}

	/**
	 * 跳转绑定页面
	 */
	public void bindProductinfo() {

		String ids = getPara("id");
		// 根据运营包大小查询,后期优化
		// String operatorType111 = getPara("operatorType");
		// String size111 = getPara("size");
		String productName = getPara("productName");

		// 获取已经绑定的供应商
		Record record = ProductVasproduct.dao.getBindedPid(ids);
		String pid = record.getStr("pid");
		// 所有供应商产品
		List<ProductInfo> productinfolist = ProductInfoService.service.findByPara(productName);

		setAttr("page", productinfolist);// list
		setAttr("bindProductinfo", pid);
		setAttr("ids", ids);
		setAttr("productName", productName);
		render(path + "productinfo.html");
	}

	/**
	 * 绑定保存
	 */
	public void saveBind() {
		String productinfoids = getPara();
		String ids = getPara("ids");

		ProductService.service.saveBind(productinfoids, ids);
		renderMessage("保存成功");

	}
}
